﻿using FlyingEye.Consts;
using FlyingEye.Exceptions;
using FlyingEye.Models.AccountActionResults;
using FlyingEye.MySqlApplication;
using FlyingEye.MySqlApplication.Models;
using FlyingEye.Users.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    /// <summary>
    /// 用户控制器。
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class AccountController : AbpController
    {
        private readonly IJwtBearerService _jwtService;

        private readonly ILogger<AccountController> _logger;

        private readonly IUserService _userService;

        private readonly IUserOrderService _userCreationOrderService;

        public AccountController(IJwtBearerService jwtBearerService,
            ILogger<AccountController> logger, IUserService userService, IUserOrderService userCreationOrderService)
        {
            _jwtService = jwtBearerService;
            _logger = logger;
            _userService = userService;
            _userCreationOrderService = userCreationOrderService;
        }

#if DEBUG
        [HttpGet]
        public string Test()
        {
            return "Ok";
        }
#endif

        /// <summary>
        /// 用户签名，有效期永久
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<UserSignActionResult>> SignInForever(LoginUser user)
        {
            var info = new UserSignActionResult();
            info.IsSuccess = false;

            try
            {
                var token = await _userService.LoginForeverAsync(user);
                info.Token = token;
                info.IsSuccess = true;
                info.Message = AccountConsts.SignInSuccess;
            }
            catch (UserNotExistException)
            {
                info.Message = AccountConsts.UserNameOrPasswordError;
            }
            catch (LoginFailedException)
            {
                info.Message = AccountConsts.UserNameOrPasswordError;
            }
            catch (UserPermissionDeniedException)
            {
                info.Message = AccountConsts.UserPermissionDenied;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, AccountConsts.SignInError);
                info.Message = ServerConsts.ServerError;
            }

            return Ok(info);
        }

        /// <summary>
        /// 用户签名，有效期 6s
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<UserSignActionResult>> SignIn(LoginUser user)
        {
            var info = new UserSignActionResult();
            info.IsSuccess = false;

            try
            {
                var token = await _userService.LoginAsync(user, TimeSpan.FromSeconds(6));
                info.Token = token;
                info.IsSuccess = true;
                info.Message = AccountConsts.SignInSuccess;
            }
            catch (UserNotExistException)
            {
                info.Message = AccountConsts.UserNameOrPasswordError;
            }
            catch (LoginFailedException)
            {
                info.Message = AccountConsts.UserNameOrPasswordError;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, AccountConsts.SignInError);
                info.Message = ServerConsts.ServerError;
            }

            return Ok(info);
        }
    }
}
